/**
 *NewWeibo.java
 *2012-4-26 下午09:12:33
 *Touch Android
 *http://bbs.droidstouch.com
 */
package com.droidstouch.iweibo.ui;

import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.droidstouch.iweibo.R;
import com.droidstouch.iweibo.bean.Task;
import com.droidstouch.iweibo.logic.MainService;

/**
 * @author <a href="http://bbs.droidstouch.com">Touch Android</a>
 *
 */
public class NewWeiboActivity extends Activity implements IWeiboActivity
{

	
	private static final int MAX_LIMIT=140;
	
	
	private Button btnNewWeibo;
	
	private EditText et_weibo;
	
	private TextView txtLimit;
	
	
	/* (non-Javadoc)
		 * @see android.app.Activity#onCreate(android.os.Bundle)
		 */
		@Override
		protected void onCreate(Bundle savedInstanceState)
		{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.newweibo);
			
			MainService.addActivity(this);
			init();
		}
		
		
	public void init()
	{
		btnNewWeibo = (Button) this.findViewById(R.id.btn_weibod_new);
		et_weibo = (EditText) this.findViewById(R.id.et_mblog);
		txtLimit = (TextView) this.findViewById(R.id.tv_text_limit);
		
		
		
		btnNewWeibo.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				newWeibo();
				finish();
			}
		});
		
		et_weibo.addTextChangedListener(new TextWatcher()
		{
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after)
			{
			}
			
			@Override
			public void afterTextChanged(Editable s)
			{
				txtLimit.setText(MAX_LIMIT - s.length() +"");
			}
		});
		
	}

	public void refresh(Object... params)
	{
		
	boolean isSuccess =	(Boolean) params[0];
	
	Toast.makeText(this, isSuccess?"发表成功":"发表失败", Toast.LENGTH_SHORT).show();
		
	}

	
	
	private void newWeibo()
	{
		
		String status= et_weibo.getText().toString();
		
		if(!TextUtils.isEmpty(status) && status.length() <=140)
		{
			Map<String, Object> taskParams = new HashMap<String, Object>(1);
			taskParams.put("status", status);
			Task task = new Task(Task.NEW_WEIBO, taskParams);
			
			MainService.newTask(task);
		}
		else
		{
			Toast.makeText(this, "微博内容为空或者字数超过限制", Toast.LENGTH_SHORT).show();
		}
	}
	
	
}
